package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallHotwordMapper;
import org.linlinjava.litemall.db.domain.LitemallHotword;
import org.linlinjava.litemall.db.domain.LitemallHotwordExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LitemallHotwordService {
    @Resource
    LitemallHotwordMapper mapper;

    /**
     * @Description  记录一次热词
     * @Author Riko
     * @Date 2019/10/14
     **/
    public void storeNewWord(String word) {
        LitemallHotwordExample example = new LitemallHotwordExample();
        example.or().andKeywordEqualTo(word);
        LitemallHotword litemallHotword = mapper.selectOneByExample(example);
        if (litemallHotword != null) {
            litemallHotword.setTimes(litemallHotword.getTimes() + 1);
            mapper.updateByPrimaryKeySelective(litemallHotword);
        }
        else {
            LitemallHotword hotWord = new LitemallHotword();
            hotWord.setKeyword(word);
            mapper.insertSelective(hotWord);
        }
    }

    /**
     * @Description  提取前五条热词
     * @Author Riko
     * @Date 2019/10/14
     **/
    public List<LitemallHotword> selectKeyword() {
        LitemallHotwordExample example = new LitemallHotwordExample();
        example.orderBy("times desc");
        PageHelper.startPage(1,5);
        return mapper.selectByExample(example);
    }
}
